import java.util.HashMap;
import java.util.Map;

class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
        String croak = "croak";
        int len = croak.length();
        Map<Character, Integer> map = new HashMap<>();
        for(int i = 0; i < len; i++){
            map.put(croak.charAt(i), i);
        }
        int[] hash = new int[len];
        for(int i = 0; i < croakOfFrogs.length(); i++){
            char ch = croakOfFrogs.charAt(i);
            if(ch == 'c'){
                if(hash[len - 1] > 0){
                    hash[len - 1]--;
                }
                int index = map.get(ch);
                hash[index]++;
            }else{
                int index = map.get(ch);
                if(hash[index - 1] <= 0){
                    return -1;
                }else{
                    hash[index - 1]--;
                }
                hash[index]++;
            }
        }
        for(int i = 0; i < len - 1; i++){
            if(hash[i] != 0){
                return -1;
            }
        }
        return hash[len - 1];
    }
    public int minNumberOfFrogs1(String croakOfFrogs) {
        int[] hash = new int[5];
        for(int i = 0; i < croakOfFrogs.length(); i++){
            char ch = croakOfFrogs.charAt(i);
            //遇到字符c判断
            if(ch == 'c'){
                hash[0]++;
                if(hash[4] > 0){
                    hash[4]--;
                }
            }else{
                //遇到字符r o a k
                if(ch == 'r'){
                    hash[1]++;
                    if(hash[0] <= 0){
                        return -1;
                    }else{
                        hash[0]--;
                    }
                }else if(ch == 'o'){
                    hash[2]++;
                    if(hash[1] <= 0){
                        return -1;
                    }else{
                        hash[1]--;
                    }
                }else if(ch == 'a'){
                    hash[3]++;
                    if(hash[2] <= 0){
                        return -1;
                    }else{
                        hash[2]--;
                    }
                }else{
                    hash[4]++;
                    if(hash[3] <= 0){
                        return -1;
                    }else{
                        hash[3]--;
                    }
                }
            }
        }
        return hash[4];
    }
}